package com.lg.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.cms.entity.ArticleType;
import com.lg.cms.mapper.ArticleTypeMapper;
import com.lg.cms.service.ArticleTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lg.cms.vo.LayerTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章类型表 服务实现类
 * </p>
 *
 * @author lg
 * @since 2020-09-21
 */
@Service
public class ArticleTypeServiceImpl extends ServiceImpl<ArticleTypeMapper, ArticleType> implements ArticleTypeService {
    @Autowired
    ArticleTypeMapper articleTypeMapper;
    @Override
    public Map<String, Integer> getArticleTypeMap() {
        Map<String, Integer> map = new HashMap<>();
        articleTypeMapper.selectList(null).forEach(v -> map.put(v.getAlias(), v.getId()));
        return map;
    }

    @Override
    public LayerTableVO TypeList(Integer tagsId, Integer pageNum) {
        LayerTableVO <ArticleType> ll=new LayerTableVO<>();
        IPage <ArticleType> IP=articleTypeMapper.selectPage(new Page<ArticleType>(tagsId,pageNum),null);
        ll.setData(IP.getRecords());
        ll.setCount(IP.getTotal());
        ll.setCode(0);

        return ll;
    }

    @PostConstruct
    public void init(){
        ArticleType articleType = articleTypeMapper.selectOne(
                new QueryWrapper<ArticleType>().eq("name", "文章").eq("alias", "article"));
        ArticleType imageType = articleTypeMapper.selectOne(
                new QueryWrapper<ArticleType>().eq("name", "图片").eq("alias", "image"));
        ArticleType videoType = articleTypeMapper.selectOne(
                new QueryWrapper<ArticleType>().eq("name", "视频").eq("alias", "video"));

        if(articleType == null){
            articleType = new ArticleType();
            articleType.setAlias("article");
            articleType.setName("文章");
            articleTypeMapper.insert(articleType);
        }
        if(imageType == null){
            imageType = new ArticleType();
            imageType.setAlias("image");
            imageType.setName("图片");
            articleTypeMapper.insert(imageType);
        }
        if(videoType == null){
            videoType = new ArticleType();
            videoType.setAlias("video");
            videoType.setName("视频");
            articleTypeMapper.insert(videoType);
        }
    }
}
